package com.cmc.cloud.cmclink.doc.controller.admin.special;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ContainerMaxPayloadErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.ContainerMaxPayloadConvert;
import com.cmc.cloud.cmclink.doc.entity.ContainerMaxPayloadDO;
import com.cmc.cloud.cmclink.doc.service.ContainerMaxPayloadService;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadRespVO;
import com.cmc.cloud.cmclink.doc.vo.special.containermaxpayloadvo.ContainerMaxPayloadUpdateReqVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 箱型最大载重维护前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 箱型最大载重维护")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/special/container-max-payload")
public class ContainerMaxPayloadController {

    @Resource
    private ContainerMaxPayloadService containerMaxPayloadService;

    @Operation(summary = "创建箱型最大载重维护")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:MaxPayloadSize:add')")
    public CommonResult<Void>
        createContainerMaxPayload(@Valid @RequestBody ContainerMaxPayloadCreateReqVO createReqVO) {
        containerMaxPayloadService.createContainerMaxPayload(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新箱型最大载重维护")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:MaxPayloadSize:edit')")
    public CommonResult<Void>
        updateContainerMaxPayload(@Valid @RequestBody ContainerMaxPayloadUpdateReqVO updateReqVO) {
        containerMaxPayloadService.updateContainerMaxPayload(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除箱型最大载重维护")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:MaxPayloadSize:delete')")
    public CommonResult<Void> deleteContainerMaxPayload(@PathVariable("id") Long id) {
        containerMaxPayloadService.deleteContainerMaxPayload(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得箱型最大载重维护")
    @GetMapping("/get/detail/{id}")
    @UseResponsePropertyAdvice
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ContainerMaxPayloadRespVO> getContainerMaxPayload(@PathVariable("id") Long id) {
        ContainerMaxPayloadDO containerMaxPayload = containerMaxPayloadService.getContainerMaxPayload(id);
        if (containerMaxPayload == null) {
            throw ServiceExceptionUtil.exception(ContainerMaxPayloadErrorCodeConstants.CONTAINER_NOT_EXISTED);
        }
        return CommonResult.success(ContainerMaxPayloadConvert.INSTANCE.convert(containerMaxPayload));
    }

    @Operation(summary = "获得箱型最大载重维护分页")
    @PostMapping("/page")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:MaxPayloadSize:search')")
    public CommonResult<PageResult<ContainerMaxPayloadRespVO>>
        getContainerMaxPayloadPage(@Valid @RequestBody ContainerMaxPayloadPageReqVO pageVO) {
        PageResult<ContainerMaxPayloadDO> pageResult = containerMaxPayloadService.getContainerMaxPayloadPage(pageVO);
        List<ContainerMaxPayloadRespVO> results = ContainerMaxPayloadConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }
}
